package cn.hp.beauty.controller;

import cn.hp.beauty.domain.Userinfo;
import cn.hp.beauty.service.UserinfoService;
import cn.hp.beauty.util.JwtUtil;
import cn.hp.beauty.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/user")
@Api(tags = "userinfo",value = "/userinfo")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/find")
    @PreAuthorize("hasRole('Role_admin')")
    @ApiOperation(value = "查找用户", notes = "列表查询")
    public Result find(@RequestHeader(required = false) String token, Userinfo userinfo){
        if (token != null) {
            // 模拟取出所有用户的信息
            List<Userinfo> list = userinfoService.findList(userinfo);
            System.out.println(list);
            return Result.success(list);
        } else {
            return Result.error("没有获得权限");
        }
    }

    /**
     * 新增用户信息
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    public Result insert(@RequestBody Userinfo userInfo){
        int i = userinfoService.insertUserinfo(userInfo);
        return i > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    public Result update(@RequestBody Userinfo userInfo) {
        int i = userinfoService.updateUserinfo(userInfo);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除用户数据
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    public Result deleteById(@PathVariable @ApiParam("被删除用户ID") int id) {
        int i = userinfoService.deleteById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

}